package com.boot.system.controller;

import com.boot.framework.aop.Log;
import com.boot.framework.common.R;
import com.boot.system.dto.SysRoleDTO;
import com.boot.system.service.ISysRoleService;
import com.boot.system.wrapper.SysRoleWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.service.annotation.GetExchange;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author yangfan
 * @date 2024/1/7
 * @Description
 */
@RestController
@AllArgsConstructor
@RequestMapping("/system/role")
@Tag(name = "角色管理")
public class SysRoleController {
    private final ISysRoleService sysRoleService;

    @GetMapping("page")
    @Operation(summary = "分页")
    public R page(@RequestParam Map<String, Object> params) {

        return R.ok(sysRoleService.selectPage(params));
    }

    @GetMapping("list")
    @Operation(summary = "集合")
    public R list(Map<String, Object> params) {
        return R.ok(sysRoleService.selectList(params));
    }


    @PostMapping
    @Log(title = "角色管理", content = "新增")
    @Operation(summary = "新增")
    public R insert(@RequestBody SysRoleDTO sysRoleDTO) {
        return R.ok(sysRoleService.save(SysRoleWrapper.build().dtoEntity(sysRoleDTO)));
    }


    @PutMapping
    @Log(title = "参数管理", content = "修改")
    @Operation(summary = "修改")
    public R update(@RequestBody SysRoleDTO sysRoleDTO) {
        return R.ok(sysRoleService.updateById(SysRoleWrapper.build().dtoEntity(sysRoleDTO)));
    }


    @GetMapping
    @Operation(summary = "查看")
    public R view(@RequestParam String id) {
        return R.ok(sysRoleService.getById(id));
    }

    @DeleteMapping
    @Log(title = "参数管理", content = "删除")
    @Operation(summary = "删除")
    public R delete(String ids) {
        return R.ok(sysRoleService.removeBatchByIds(Arrays.asList(StringUtils.split(ids, ","))));
    }

    @GetMapping("dropList")
    @Operation(summary = "下拉集合")
    public R select() {
        return R.ok(sysRoleService.dropList());
    }

    @PostMapping("/list/ids")
    @Operation(summary = "集合")
    R<List<SysRoleDTO>> selectByIds(@RequestBody List<String> ids) {
        return R.ok(sysRoleService.selectByIds(ids));
    }
}
